package com.tencent.iov.cowin.befsale.clue.service.cus.impl;

import com.github.pagehelper.Page;
import com.tencent.iov.cowin.befsale.clue.controller.cus.assembler.DealerCustomerTagRelAssembler;
import com.tencent.iov.cowin.befsale.clue.controller.cus.vo.request.AddDealerCustomerTagRelRequest;
import com.tencent.iov.cowin.befsale.clue.controller.cus.vo.request.DeleteDealerCustomerTagRelRequest;
import com.tencent.iov.cowin.befsale.clue.controller.cus.vo.request.QueryDealerCustomerTagRelRequest;
import com.tencent.iov.cowin.befsale.clue.controller.cus.vo.request.UpdateDealerCustomerTagRelRequest;
import com.tencent.iov.cowin.befsale.clue.domain.cus.po.DealerCustomerTagRel;
import com.tencent.iov.cowin.befsale.clue.repository.cus.DealerCustomerTagRelManager;
import com.tencent.iov.cowin.befsale.clue.service.cus.DealerCustomerTagRelService;
import com.tencent.iov.cowin.befsale.clue.value.enums.DeletedEnum;
import com.software.boot.common.model.dao.PageRequest;
import com.software.boot.common.model.dao.PageResponse;
import com.software.boot.common.util.BeanUtil;
import com.software.boot.common.util.EmptyUtils;
import com.software.boot.common.util.ValidateUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Describe: 经销商客户标签关联表_Service实现层
 * @author: xiamu
 * @created: 2021-12-21 17:29:59
 * @version: 1.0
 */
@Service
public class DealerCustomerTagRelServiceImpl implements DealerCustomerTagRelService {

    @Autowired
    private DealerCustomerTagRelManager dealerCustomerTagRelManager;

    @Override
    public DealerCustomerTagRel getDetailById(Long id) {
        //验证请求参数
        ValidateUtils.checkObjNull(id, "请求参数不能为空");
        ValidateUtils.checkIntNotNullAndGTZero(id.intValue(), "ID不能为空或小于0");
        //处理业务
        DealerCustomerTagRel dealerCustomerTagRel = dealerCustomerTagRelManager.getDetailById(id);
        if (EmptyUtils.isNotEmpty(dealerCustomerTagRel)) {
            return BeanUtil.copy(dealerCustomerTagRel, DealerCustomerTagRel.class);
        }
        //返回结果
        return null;
    }

    @Override
    public int insert(AddDealerCustomerTagRelRequest request) {
        //验证请求参数
        //处理业务
        DealerCustomerTagRel dealerCustomerTagRel = DealerCustomerTagRelAssembler.transformVOToPO(request, DealerCustomerTagRel.class);
        return dealerCustomerTagRelManager.insert(dealerCustomerTagRel);
    }

    @Override
    public int insertBatch(List<QueryDealerCustomerTagRelRequest> requests) {
        // 验证请求参数
        //处理业务
        List<DealerCustomerTagRel> dealerCustomerTagRels = DealerCustomerTagRelAssembler.transformPOToResponseVoList(requests, DealerCustomerTagRel.class);
        return dealerCustomerTagRelManager.insertBatch(dealerCustomerTagRels);
    }

    @Override
    public int update(UpdateDealerCustomerTagRelRequest request) {

        //验证请求参数
        //处理业务
        // 返回对象
        DealerCustomerTagRel dealerCustomerTagRel = DealerCustomerTagRelAssembler.transformVOToPO(request, DealerCustomerTagRel.class);
        return dealerCustomerTagRelManager.update(dealerCustomerTagRel);
    }

    @Override
    public int delete(DeleteDealerCustomerTagRelRequest request) {
        Long id = request.getId();
        //验证请求参数
        ValidateUtils.checkObjNull(id, "请求参数不能为空");
        ValidateUtils.checkIntNotNullAndGTZero(id.intValue(), "ID不能为空或小于0");
        //处理业务
        DealerCustomerTagRel dealerCustomerTagRel = new DealerCustomerTagRel();
        dealerCustomerTagRel.setId(id);
        dealerCustomerTagRel.setDeleted(DeletedEnum.VANISH.getCode());
        return dealerCustomerTagRelManager.update(dealerCustomerTagRel);
    }

    @Override
    public List<DealerCustomerTagRel> getListByParam(QueryDealerCustomerTagRelRequest request) {
        //验证请求参数
        //可以设置排序
        //处理业务
        DealerCustomerTagRel dealerCustomerTagRel = DealerCustomerTagRelAssembler.transformVOToPO(request, DealerCustomerTagRel.class);
        List<DealerCustomerTagRel> queryList = dealerCustomerTagRelManager.getListByParam(dealerCustomerTagRel);
        // 返回结果
        if (CollectionUtils.isNotEmpty(queryList)) {
            return BeanUtil.copyByList(queryList, DealerCustomerTagRel.class);
        }
        return new ArrayList<>();
    }

    @Override
    public PageResponse<DealerCustomerTagRel> getPageByParam(PageRequest<QueryDealerCustomerTagRelRequest> request) {
        //验证请求参数
        ValidateUtils.checkObjNull(request, "请求参数不能为空");
        //可以设置排序 query.orderByCreatedTime(true);
        //处理业务
        PageRequest<DealerCustomerTagRel> pageRequest = DealerCustomerTagRelAssembler.transformVOToPOPage(request, DealerCustomerTagRel.class);
        RowBounds rowBounds = new RowBounds(pageRequest.getPageNum(), pageRequest.getPageSize());
        Page<DealerCustomerTagRel> queryPageList = dealerCustomerTagRelManager.getPageByParam(pageRequest.getParamData(), rowBounds);
        PageResponse<DealerCustomerTagRel> pageResponse = new PageResponse<>();
        pageResponse.converPageResponse(pageResponse, queryPageList, request);
        pageResponse.setDataList(BeanUtil.copyByList(queryPageList.getResult(), DealerCustomerTagRel.class));
        //返回结果
        return pageResponse;
    }

    @Override
    public DealerCustomerTagRel getOneByParam(QueryDealerCustomerTagRelRequest request) {
        DealerCustomerTagRel dealerCustomerTagRel = DealerCustomerTagRelAssembler.transformVOToPO(request, DealerCustomerTagRel.class);
        return dealerCustomerTagRelManager.getOneByParam(dealerCustomerTagRel);
    }
}
